package com.harmonyos.studyharmonyos.provider;

import com.harmonyos.studyharmonyos.ResourceTable;
import com.harmonyos.studyharmonyos.bean.SampleItem;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class SampleItemProvider extends BaseItemProvider {
    private List<SampleItem> list;
    private AbilitySlice slice;

    public SampleItemProvider(List<SampleItem> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        Component cpt;
        SettingHolder holder;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_sample, null, false);
            holder = new SettingHolder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = convertComponent;
            holder = (SettingHolder) cpt.getTag();
        }
        SampleItem sampleItem = list.get(position);
        holder.strText.setText(sampleItem.getName());
        return cpt;
    }

    public static class SettingHolder {
        Text strText;

        SettingHolder(Component component) {
            strText = (Text) component.findComponentById(ResourceTable.Id_item_index);
        }
    }
}
